package com.example.myapplication.danmu

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.FrameLayout
import androidx.databinding.DataBindingUtil
import com.example.myapplication.R
import com.example.myapplication.databinding.ActivityPlayBinding
import com.example.myapplication.databinding.ViewDanmuChannelBinding

/**
 * @author: rongfeng
 * @date: 2022/3/16
 * @description
 */
class DanmuManagerView @JvmOverloads constructor(
        context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr), DanmuChannelController.ChannelController {

    var binding : ViewDanmuChannelBinding? = null

    init {
           binding = DataBindingUtil.bind(
            View.inflate(context, R.layout.view_danmu_channel, this))
    }

    val danmuList = ArrayList<String>()
    val mDanmuChannelController1 by lazy { DanmuChannelController(binding!!.rl1, 1, this) }
    val mDanmuChannelController2 by lazy { DanmuChannelController(binding!!.rl2, 2, this) }

    fun starLoopDanmu(danmu: String) {
        danmuList.add(danmu)
        if (!mDanmuChannelController1.isLooping) {
            loopDanmu(1)
        } else if (!mDanmuChannelController2.isLooping) {
            loopDanmu(2)
        }
    }

    override fun loopDanmu(channel: Int) {
        if (danmuList.size > 0) {
            val data = danmuList[0]
            if (channel == 1) {
                mDanmuChannelController1.setData(data)
                danmuList.removeAt(0)
            } else if (channel == 2) {
                mDanmuChannelController2.setData(data)
                danmuList.removeAt(0)
            }
        }
    }
}